﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Common.Entity;
using ESOL.Business.Fin;
using ESOL.WinUI.MasterData;
using ESOL.Common;
using ESOL.WinUI.Sales;

namespace ESOL.WinUI.Finance
{
    public partial class frmManageARAccount : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private MstCustomerEntity mSelectedCustomer;
        private FinCustomerAccountEntity mSelectedCustomerAccount;
        #endregion

        #region Contructor
        public frmManageARAccount()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private string GetCondition()
        {
            string condition = "";
            if (mSelectedCustomer != null)
            {
                condition += " AND CA.CUSTOMER_ID = '" + mSelectedCustomer.CustomerId + "' ";
            }
            if (dtpFromDate.EditValue!=null)
            {
                condition += " AND CA.AMOUNT_DATE >= CONVERT(DATETIME,'" + dtpFromDate.EditValue.ToString() + "')";
            }
            if (dtpEndDate.EditValue!=null)
            {
                condition += " AND CA.AMOUNT_DATE <= CONVERT(DATETIME,'" + dtpEndDate.EditValue.ToString() + "')";
            }
            return condition;
        }
        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "C.CUSTOMER_NAME DESC";
            DataTable dt = FinCustomerAccountItemBs.GetTotalByCondition(condition, orderBy, rowCount);
            gvMain.DataSource = dt;
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            BindDataGrid();
            base.OnShown(e);
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            if (string.IsNullOrEmpty(dr["CUSTOMER_ID"].ToString())==false)
            {
                mSelectedCustomerAccount = FinCustomerAccountBs.GetByCustomerId(dr["CUSTOMER_ID"].ToString().Trim());
            }
        }

        private void txtCustomer_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainCustomerQuery frm = new frmMaintainCustomerQuery();
            frm.ShowAllCustomer = true;
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedCustomer != null)
            {
                txtCustomer.Text = frm.SelectedCustomer.CustomerName;
                mSelectedCustomer = frm.SelectedCustomer;
            }
            else
            {
                txtCustomer.Text = "";
                mSelectedCustomer = null;
            }
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        protected override void Click_View()
        {
            if (mSelectedCustomerAccount == null)
            {
                MessageBox.Show("请选择一个客户总账！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainARAccountDetail frm = new frmMaintainARAccountDetail(mSelectedCustomerAccount);
            frm.ShowDialog();
        }

        #endregion

    }
}
